package 剑指offer;

import com.sun.org.apache.xml.internal.utils.StringBufferPool;

import java.util.HashSet;
import java.util.Set;

public class _38 {
    Set<String> set = new HashSet<String>();
    StringBuilder  stringBuilder = new StringBuilder();
    public String[] permutation(String s) {
        char[] chars = s.toCharArray();
        boolean[] flag = new boolean[s.length()];
        fun(chars,flag);
        String[] strings = new String[set.size()];
        int i = 0;
        for(String s1 : set){
            strings[i] = s1;
            i++;
        }
        return strings;
    }
    public void fun(char[] chars, boolean[] flag){
        if(stringBuilder.length() == chars.length){
            set.add(new String(stringBuilder));
        }
        for(int i = 0; i < chars.length; i++){
            if(flag[i] == true){
                continue;
            }
            stringBuilder.append(chars[i]);
            flag[i] = true;
            fun(chars,flag);
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            flag[i] = false;
        }
    }
}
